package com.yungu.swift.utils;

public class LocationUtils {

    private static double EARTH_RADIUS = 6371.393;

    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    /**
     * 计算两个经纬度之间的距离
     *
     * @param oriLng
     * @param oriLat
     * @param desLng
     * @param desLat
     * @return 米
     */
    public static double getLineDistance(double oriLng, double oriLat, double desLng, double desLat) {
        double radOriLat = rad(oriLat);
        double radDesLat = rad(desLat);
        double a = radOriLat - radDesLat;
        double b = rad(oriLng) - rad(desLng);
        double distance = 2 * Math.asin(Math.sqrt(Math.abs(Math.pow(Math.sin(a / 2), 2) +
                Math.cos(radOriLat) * Math.cos(radOriLat) * Math.pow(Math.sin(b / 2), 2))));
        distance = distance * EARTH_RADIUS;
        distance = Math.round(distance * 1000);
        return distance;
    }
}
